﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MyATM.DataAccess;
using MyATM.Entity;
using MyATM.Common;

namespace MyATM.Bussiness
{
    public class MainSystem
    {
        //Private variables
        private AccountDAO accountDaoObject = null;
        private CardDA cardDaObject = null;
        private BankOfficerDA bankOfficerDaObject = null;
        private Utilities utilities = null;
        /// <summary>
        /// Constructor
        /// </summary>
        public MainSystem()
        {
            accountDaoObject = new AccountDAO();
            cardDaObject = new CardDA();
            bankOfficerDaObject = new BankOfficerDA();
            utilities = new Utilities();
        }
        /// <summary>
        /// Xac thuc card ATM
        /// </summary>
        /// <param name="cardId">Ma card</param>
        /// <param name="cardPin">Ma Pin</param>
        /// <returns></returns>
        public bool AuthenticateCard(string cardId, string cardPin)
        {
            try
            {
                bool result = cardDaObject.AuthenticateCard(cardId, cardPin);
                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// Thay doi trang thai cua the
        /// </summary>
        /// <param name="cardId">ID cua car</param>
        /// <param name="cardStatus">trang thai cua Card</param>
        /// <returns></returns>
        public int ModifyCardSatus(string cardId, int cardStatus)
        {
            bool check = utilities.CheckValidInput(cardId, 1);
            if (!string.IsNullOrEmpty(cardId) && check && cardStatus > 0 && cardStatus < 5)
            {
                int result = accountDaoObject.ModifyCard(cardId, cardStatus);
                return result;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// Xac thuc officer
        /// </summary>
        /// <param name="officerId">Username cua nhan vien</param>
        /// <param name="officerPassword">Password cua nhan vien</param>
        /// <returns>true or false</returns>
        public bool AuthenticateBankOfficer(string officerId, string officerPassword)
        {
            bool result = false;
            if(!string.IsNullOrEmpty(officerId) && !string.IsNullOrEmpty(officerPassword))
            {
                result = bankOfficerDaObject.AuthenticateOfficer(officerId, officerPassword);
            }
            return result;
        }
        /// <summary>
        /// Them mot card moi vao he thong
        /// </summary>
        /// <param name="atmCard">ID card</param>
        /// <returns>chuoi thong bao</returns>
        public string AddNewCard(Card atmCard)
        {
            //Check null
            if (atmCard == null)
            {
                return "Object null!";
            }
            else
            {
                bool isCardExist = accountDaoObject.ValidCardId(atmCard.CardId);
                if (isCardExist == false)
                {
                    int result = accountDaoObject.AddNewCard(atmCard);
                    if (result == 1)
                    {
                        return "Successfully!";
                    }
                    else
                    {
                        return "Failed!";
                    }
                }
                else
                {
                    return "Card is exist!";
                }
            }
        }
        /// <summary>
        /// Lay ve thong tin trang thai cua the ATM
        /// </summary>
        /// <param name="cardId">ID cua the</param>
        /// <returns>
        ///   1. Normal
        ///   2. Block Permanent
        ///   3. Block Tempature
        ///   4. Card Expire Or Not valid
        /// </returns>
        public int GetCardStatus(string cardId)
        {
            bool checkValidInput = utilities.CheckValidInput(cardId, 1);
            if (!string.IsNullOrEmpty(cardId) && checkValidInput)
            {
                int cardStatus = cardDaObject.GetStatus(cardId);
                return cardStatus;
            }
            else
            {
                return 4;
            }
        }

        public bool ValidCard(string cardId)
        {
            if (!string.IsNullOrEmpty(cardId))
            {
                bool result = cardDaObject.ValidateCard(cardId);
                return result;
            }
            else
            {
                return false;
            }
        }

        public bool ValidAccount(string accountId)
        {
            if(!string.IsNullOrEmpty(accountId))
            {
                bool result = accountDaoObject.ValidAccountId(accountId);
                return result;
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// Khoa tam thoi mot card
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        public bool BlockCard(string cardId)
        {
            if(cardDaObject.BlockCard() == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
